package com.ht.pms.core.service.job;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

import com.ht.pms.core.common.cache.SalaryServiceCache;
import com.ht.pms.core.common.exception.ExcelParseException;
import com.ht.pms.core.common.exception.ParamException;
import com.ht.pms.core.dto.Result;
import com.ht.pms.core.dto.poi.SalaryUploadParam;
import com.ht.pms.core.service.SalaryExcelDataService;

public class ExcelImportThread implements Runnable {
	private static final Logger LOGGER = LoggerFactory.getLogger(ExcelImportThread.class);

	private SalaryExcelDataService salaryExcelDataService;
	
	private SalaryServiceCache salaryServiceCache;

	private SalaryUploadParam req;
	
	private Long processCode;

	public ExcelImportThread(SalaryUploadParam req, Long processCode, SalaryExcelDataService salaryExcelDataService,  SalaryServiceCache salaryServiceCache) {
		this.req = req;
		this.processCode = processCode;
		this.salaryExcelDataService = salaryExcelDataService;
		this.salaryServiceCache = salaryServiceCache;
	}

	@Override
	public void run() {
		Result result = null;
		LOGGER.info("[文件上传] 上传处理流程开始   req：[{}] ", this.req);
		long start = System.currentTimeMillis();
		try {
			result = salaryExcelDataService.uploadSalaryFile(req);
		} catch (ParamException e) {
			e.printStackTrace();
			result = new Result();
			result.setCodeDesc(e.getErrorMsg());
			result.setReturnCode(e.getErrorCode());
			result.setMsg(e.getErrorMsg());
		} catch (SAXException e) {
			e.printStackTrace();
			result = Result.buildFail("解析excel文件出错");
		} catch (ExcelParseException e) {
			e.printStackTrace();
			result = new Result();
			result.setCodeDesc(e.getErrorMsg());
			result.setReturnCode(e.getErrorCode());
			result.setMsg(e.getErrorMsg());
		} catch (Exception e) {
			e.printStackTrace();
			result = Result.buildFail("执行失败，系统异常!");
		}
		LOGGER.info("[文件上传] 上传处理流程结束   result：[{}], 用时[{}ms] ", result, (System.currentTimeMillis() - start));
		salaryServiceCache.cacheUploadProcessResult(result, processCode);
	}
}
